//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Graphics/Resources/Font.h"
#include "Graphics/Loaders/VectorFontManager.h"

namespace Engine
{
namespace Graphics
{


	//
	// Vector Font
	//

	class VectorFont : public IFont
	{
		friend struct VectorFontManager;
		friend struct FontLoader;

	// types
	public:
		struct VectorFontLoadParams : ResLoadParams
		{
		// methods
		public:
			VectorFontLoadParams () : ResLoadParams( Signature() )
			{}

			static signature_t Signature ()
			{
				return CompileTime::TSignature8< 'G', 'V', 'E', 'C', 'F', 'O', 'N', 'T' >::value;
			}
		};


		typedef VectorFontManager::tex_atlas_id_t	glyph_index_t;
		typedef VectorFontManager::FontFacePtr		font_face_t;


		struct VecGlyph : Glyph
		{
			Ptr< Texture >	texture;
			bool			loaded;
			///
			VecGlyph () : Glyph(), loaded(false) {}

			explicit
			VecGlyph (float advanceX) : Glyph(advanceX), loaded(false) {}

			VecGlyph & operator = (const VecGlyph &right)	{ MemCopy( this, &right, sizeof(*this) ); return *this; }
		};
		

	protected:

		typedef static_array< VecGlyph, 256 >	glyph_array_t;


		struct Level
		{
			glyph_array_t	glyphs;
			///
			Level () {}
		};


		typedef map< uint, Level >		glyph_levels_t;		// key - height

		struct GlyphReader;
		friend struct GlyphReader;


	// variables
	protected:
		font_face_t		_fontFreeType;
		glyph_levels_t	_levels;
		float			_baseLineTopMargin;
		uint			_heightForMinLevel;


	// methods
	public:
		VectorFont (const EngineSubSystemsRef ess, EResource::type resType = EResource::VECTOR_FONT);
		~VectorFont ();
		
		void Draw (const FontBatch &params, const vec2 &pos, uni_c_string text, bool scissor) override;
		void Draw (const FontBatch &params, const rect &region, uni_c_string text, bool scissor) override;
		
		void Draw (const FontBatch &params, const vec2 &pos, const ext_string &text, bool scissor) override;
		void Draw (const FontBatch &params, const rect &region, const ext_string &text, bool scissor) override;

		void GetTextDimensions (const FontBatch &params, uni_c_string text, OUT vec2 &dim, OUT usize &lineLength) override;
		
		void GetFormattedText (const FontBatch &params, float maxLineWidth, const ext_string &text,
								const LocaleCharacters &localeChars, OUT ext_string &formattedText, OUT vec2 &dim) override;

		void LoadGlyphs (uni_c_string text, uint height);

		float GetScaleForHeight (uint heightInPixels) const;

		bool PreloadSymbols (uint height, char first, char last);

		bool GetTexturesForLevel (uint height, array<TexturePtr> &textures);

		
		// Resource //
		virtual bool SwapData (const ResourcePtr &) override;
		
		virtual bool IsLoadedWithParams (const ResLoadParams &params) const override;
		
		virtual bool IsValid () const override;


		static VectorFontPtr	New (const EngineSubSystemsRef ess);

		static VectorFontLoadParams	GetDefaultLoadParams ();


	private:
		float _CalcBaseLineTopMargin (uint height) const;
		uint  _CalcHeight (float k, float scale) const;
		bool  _LoadGlyph (uint8 symbol, uint height, Texture *&tex);

		const VecGlyph &  _GetGlyph (uint8 symbol, uint height) const;
	};


}	// Graphics
}	// Engine